<?php
/**
 * @name IndexController
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
error_reporting(E_ALL^E_NOTICE^E_WARNING);
class SellerController extends Yaf_Controller_Abstract {

	public function getSellerListAction(){
		$request=json_decode(file_get_contents("php://input"),true);
		$categoryid=$request['categoryid'] ? $request['categoryid'] : $_REQUEST['categoryid'];
		$id=$request['id'] ? $request['id'] : $_REQUEST['id'];
		$name=$request['name'] ? $request['name'] : $_REQUEST['name'];
		$page = $request['page'] ? $request['page'] : 1;
		$pagesize = $request['pagesize'] ? $request['pagesize'] : 10;
	
		$where = [];
		
		if($categoryid){
			$where['categoryid = ?'] = $categoryid;
		}
		
		if($id){
			$where['id = ?'] = $id;
		}
		
		if($name){
			$where["name like '%?%'"] = $name;
		}
		
		$model = new SellerModel();
		$list = $model->getList($where, $page, $pagesize);
		$ids = [];
		foreach($list as $val){
			$ids[] = $val['categoryid'];
		}
		
		$categoryModel = new CategoryModel();
		$categoryList = $categoryModel->getListByIds($ids);
		
		foreach($list as &$val){
			$val['category'] = $categoryList[$val['categoryid']]['catname'];
		}
		
		$count = $model->getCount($where);
		
		echo json_encode(['total' => $count, 'list' => $list]);
	}
	
	public function getSellerAdminListAction(){
		$request=json_decode(file_get_contents("php://input"),true);
		$seller_id=$request['seller_id'] ? $request['seller_id'] : $_REQUEST['seller_id'];
		$username=$request['username'] ? $request['username'] : $_REQUEST['username'];
		$page = $request['page'] ? $request['page'] : 1;
		$pagesize = $request['pagesize'] ? $request['pagesize'] : 10;
	
		$where = [];
		
		if($seller_id){
			$where['seller_id = ?'] = $seller_id;
		}
		
		if($username){
			$where["username like '%?%'"] = $username;
		}
		
		$model = new SellerAdminModel();
		$list = $model->getList($where, $page, $pagesize);
		
		foreach($list as &$val){
			$val['dateline'] = date('Y-m-d H:i:s', $val['dateline']);
		}
		
		$count = $model->getCount($where);
		
		echo json_encode(['total' => $count, 'list' => $list]);
	}
	
	public function getAllSellerListAction(){

		$model = new SellerModel();
		$list = $model->getList([], 0, 0);
		
		echo json_encode($list);
	}
	
	public function getSellerAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$id = $request['id'];
		
		$model = new SellerModel();
		$where = [];
		if($id){
			$where['id = ?'] = $id;
		}
		
		$rs = $model->getOne($where);
		echo json_encode($rs);
	}
	
	public function getSellerAdminAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$id = $request['id'];
		
		$model = new SellerAdminModel();
		$where = [];
		if($id){
			$where['id = ?'] = $id;
		}
		
		$rs = $model->getOne($where);
		unset($rs['password']);
		echo json_encode($rs);
	}
	
	public function saveSellerAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		if(!$request) return;
		$model = new SellerModel();
		
		$data = [
			'name'              => $request['name'],
			'type'              => 1,
			'address'           => $request['address'],
			'point'             => $request['point'],
			'thumb'             => $request['thumb'],
			'mobile'            => $request['mobile'],
			'contact'           => $request['contact'],
			'business_time'     => $request['business_time'],
			'dispatching_price' => $request['dispatching_price'],
			'send_price'        => $request['send_price'],
			'categoryid'        => $request['categoryid'],
		];

		if($request['id']){
			$where['id = ?'] = $request['id'];
			
			$rs = $model->save($data, $where);
		}else{
			
			$rs = $model->add($data);
		}
		echo json_encode($rs);
	}
	
	public function saveSellerAdminAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		if(!$request) return;
		$model = new SellerAdminModel();
		
		$data = [
			'username'          => $request['username'],
			'password'          => md5($request['password']),
		];

		if($request['seller_id']){
			$data['seller_id'] = $request['seller_id'];
		}
		
		if($request['id']){
			$where['id = ?'] = $request['id'];
			
			$rs = $model->save($data, $where);
		}else{
			$data['dateline'] = time();
			$rs = $model->add($data);
		}
		echo json_encode($rs);
	}
	
	public function delSellerAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$model = new SellerModel();

		if($request['id']){
			$where['id = ?'] = $request['id'];
			
			$rs = $model->delete($where);
			echo json_encode(['message' => '删除成功']);
		}else{
			echo json_encode(['message' => '删除失败']);
		}
		
	}
	
	public function delSellerAdminAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$model = new SellerAdminModel();

		if($request['id']){
			$where['id = ?'] = $request['id'];
			
			$rs = $model->delete($where);
			echo json_encode(['message' => '删除成功']);
		}else{
			echo json_encode(['message' => '删除失败']);
		}
		
	}
	
	public function getCategoryListAction(){
		$info=json_decode(file_get_contents("php://input"),true);
		$catid=$info['catid'] ? $info['catid'] : $_REQUEST['catid'];
		$page = $request['page'] ? $request['page'] : 1;
		$pagesize = $request['pagesize'] ? $request['pagesize'] : 10;
		
		//$catid=$_REQUEST['catid'] ? $_REQUEST['catid'] : '';
		$model = new CategoryModel();
		
		
		if($catid){
			$where['catid = ?']=$catid;
		}else{
			$where='';
		}
		
		$list = $model->getList($where, $page, $pagesize);
		
		$count = $model->getCount($where);
		echo json_encode(['total' => $count['total'], 'list' => $list]);
	}
	
	public function getAllCategoryAction(){
		$model = new CategoryModel();
		$where = [];
		$list = $model->getList($where, 0, 0);

		echo json_encode($list);
	}
	
	public function getCategoryAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$catid = $request['catid'];
		
		$model = new CategoryModel();
		$where = [];
		if($catid){
			$where['catid = ?'] = $catid;
		}
		$rs = $model->getOne($where);
		echo json_encode($rs);
	}
	
	public function saveCategoryAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		if(!$request) return;
		$model = new CategoryModel();
		
		$data['catname'] = $request['catname'];
		$data['listorder'] = $request['listorder'];
		$data['icon'] = $request['icon'];
		if($request['catid']){
			$where['catid = ?'] = $request['catid'];
			
			$rs = $model->save($data, $where);
		}else{
			$rs = $model->add($data);
		}
		echo json_encode($rs);
	}
	
	public function delCategoryAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$model = new CategoryModel();

		if($request['catid']){
			$where['catid = ?'] = $request['catid'];
			
			$rs = $model->delete($where);
			echo json_encode(['message' => '删除成功']);
		}else{
			echo json_encode(['message' => '删除失败']);
		}
		
	}
	
}
